const http = require("http");
const fs = require("fs");

const server = http.createServer((req,res) => {
    let {pathname} = new URL(req.url, "http://127.0.0.1:8080")

    if(pathname==="/") {
        pathname = "/index.html"
    }
    console.log(__dirname)
    const publicDir = __dirname + "/public"
    const filePath = publicDir + pathname
    fs.readFile(filePath,(error,data) => {
        if(error) {
            res.end("<h1>404 NOt Found</h1>")
        } else {
            res.end(data)
        }
    })
    
})






server.listen(8080,() => {
    console.log("启动服务器: http://127.0.0.1:8080")
})